var header_h, NVTabBar, ajpush, ns, app;
app = new Vue({
    el: '#app',
    data: {
        footers: [{
            title: '首页',
            name: 'dl_home_index_frm',
            is_active: true,
            normal: 'widget://image/icons/index.png',
            highlight: 'widget://image/icons/index_active.png',
            selected: 'widget://image/icons/index_active.png',
            icon_width: 25,
            icon_height: 25,
            bd_margin_bottom: -2,
            color_normal: '#999999',
            color_selected: '#ea7825'
        }, {
            title: '商城',
            name: 'dl_home_shop_frm',
            is_active: false,
            normal: 'widget://image/icons/shop.png',
            highlight: 'widget://image/icons/shop_active.png',
            selected: 'widget://image/icons/shop_active.png',
            icon_width: 25,
            icon_height: 25,
            bd_margin_bottom: -2,
            color_normal: '#999999',
            color_selected: '#ea7825'
        }, {
            title: '服务',
            name: 'dl_homeservice_frm',
            is_active: false,
            normal: 'widget://image/icons/fuwu.png',
            highlight: 'widget://image/icons/fuwu_active.png',
            selected: 'widget://image/icons/fuwu_active.png',
            icon_width: 25,
            icon_height: 25,
            bd_margin_bottom: -2,
            color_normal: '#999999',
            color_selected: '#ea7825'
        }, {
            title: '我的',
            name: 'dl_home_self_frm',
            is_active: false,
            normal: 'widget://image/icons/mine.png',
            highlight: 'widget://image/icons/mine_active.png',
            selected: 'widget://image/icons/mine_active.png',
            icon_width: 25,
            icon_height: 25,
            bd_margin_bottom: -2,
            color_normal: '#999999',
            color_selected: '#ea7825'
        }],
        page_title: '首页',
        device_id: '',
        device_type: '',
        show_room: true,
        show_title: false,
        show_cart_icon: true,
        show_chat_icon: true,
        show_search_icon: true,
        show_setting: false,
        show_search: false,
        form: {
            id: 0,
            type: 0
        },
        xiaoquname: '',
        xiaoqulist: [],
        cartcount: 0
    },
    methods: {
        open_cart: function() {
            var that = this;
            ns.openWin('dl_gouwuche_frm', '购物车', null, {
                ismain: true,
                needlogin: true
            });
        },
        open_chat: function() {
            var that = this;
            ns.openWin('dl_messagelist_frm', '消息中心', null, {
                ismain: true
            });
        },
        open_search: function() {
            var that = this;
            var delay = 0;
            if (api.systemType != 'ios') {
                delay = 100;
            }
            api.openWin({
                name: 'dl_searchbar_frm',
                url: './html/dl_searchbar_frm.html',
                delay: delay,
                slidBackEnabled: false,
                vScrollBarEnabled: false,
                pageParam: {
                    searchtype: 1,
                },
                rect: {
                    x: 0,
                    y: 0,
                    w: 'auto',
                    h: 'auto',
                },
                animation: {
                    type: 'movein',
                    subType: 'from_bottom'
                }
            });
        },
        open_xiaoqu: function() {
            var that = this;
            ns.openWin('myxiaoqu_frm', '我的小区', null, {
                needlogin: true,
                ismain: true
            });
        },
        open_setting: function() {
            api.sendEvent({
                name: 'open_setting'
            });
        },
        open_popup: function() {
            api.sendEvent({
                name: 'open_popup'
            });
        },
        open_win: function(title, name) {
            var that = this;
            if (name == '') {
                return;
            }
            ns.openWin(name, title, {
                ismain: true
            });
        },
        get_menus: function() {
            var that = this;
            var list = [];
            for (var i = 0; i < that.footers.length; i++) {
                var item = that.footers[i];
                var menu = {
                    w: api.winWidth / 4.0,
                    bg: {
                        marginB: item.bd_margin_bottom,
                    },
                    iconRect: {
                        w: item.icon_width,
                        h: item.icon_height,
                    },
                    icon: {
                        normal: item.normal,
                        highlight: item.highlight,
                        selected: item.selected
                    },
                    title: {
                        text: item.title,
                        size: 12.0,
                        normal: item.color_normal,
                        selected: item.color_selected,
                        marginB: 6.0
                    }
                }
                list.push(menu);
            };
            return list;
        },
        open_menu: function() {
            var that = this;
            NVTabBar.open({
                    styles: {
                        bg: '#fff',
                        h: 60,
                        dividingLine: {
                            width: 0.5,
                            color: '#eeeeee'
                        },
                        badge: {
                            bgColor: '#ff0',
                            numColor: '#fff',
                            size: 6.0,
                            centerX: 40,
                            centerY: 6
                        },
                    },
                    items: that.get_menus(),
                    selectedIndex: 0
                },
                function(ret, err) {
                    NVTabBar.bringToFront();
                    if (ret.eventType == 'click') {
                        that.openFrame(that.footers[ret.index]);
                    }
                    NVTabBar.bringToFront();
                }
            );
        },
        openFrame: function(item, lefttype) {
            var that = this;
            lefttype = lefttype || 0;
            if (item.name == 'dl_home_index_frm') {
                that.show_room = true;
                that.show_title = false;
                that.show_cart_icon = true;
                that.show_chat_icon = true;
                that.show_search_icon = true;
                that.show_setting = false;
                that.show_search = false;
            } else if (item.name == 'dl_home_shop_frm') {
                that.show_room = false;
                that.show_title = false;
                that.show_cart_icon = true;
                that.show_search_icon = false;
                that.show_setting = false;
                that.show_search = true;
            } else {
                that.show_room = false;
                that.show_title = true;
                that.show_cart_icon = false;
                that.show_search_icon = false;
                that.show_setting = false;
                that.show_chat_icon = false;
                that.show_search = false;
            }
            for (var i = 0; i < that.footers.length; i++) {
                that.footers[i].is_active = false;
            }
            item.is_active = true;
            that.page_title = item.title;
            var header = $api.byId('aui-header');
            $api.fixStatusBar(header);
            var headerH = $api.offset(header).h;
            var frameH = api.winHeight - headerH - 60;
            api.openFrame({
                name: item.name,
                url: './html/' + item.name + '.html',
                bounces: false,
                useWKWebView: true,
                rect: {
                    x: 0,
                    y: headerH,
                    w: 'auto',
                    h: 'auto',
                    marginBottom: 60
                },
                pageParam: {
                    header_h: headerH,
                    footer_h: 60,
                    lefttype: lefttype
                }
            })
        },
        //保存极光用户注册id
        save_jg_cid: function() {
            var that = this;
            ajpush.getRegistrationId(function(ret, err) {
                if (ret && ret.id) {
                    that.device_id = ret.id;
                    ns.post({
                        device_id: that.device_id,
                        device_type: that.device_type,
                        action: 'savedeviceid'
                    }, function(succeed, data, err) {

                    }, {
                        ismain: true
                    });
                }
            });
        },
        //初始化
        jp_init: function() {
            var that = this;
            if (that.device_type == 'android') {
                ajpush.init(function(ret, err) {});
            }
            setTimeout(function() {
                that.jp_setListener();
                that.jp_Add_Listener();
                that.jp_Resume_Push();
                that.save_jg_cid();
                ajpush.setBadge({
                    badge: 0
                });
            }, 2000);
        },
        //APP界面打开状态获取消息
        jp_setListener: function() {
            var that = this;
            ajpush.setListener(function(ret, err) {
                if (ret && ((ret.title && that.device_type == 'ios') || that.device_type == 'android')) {
                    var rep = ret.extra;
                    api.getPrefs({
                        key: 'uid'
                    }, function(res, err) {
                        if (res.value) {
                            ajpush.setBadge({
                                badge: 0
                            });
                            that.jp_process(rep, true);
                        } else {
                            api.sendEvent({
                                name: 'onlogin'
                            });
                        }
                    });
                }
            });
        },
        //APP界面隐藏状态获取消息
        jp_Add_Listener: function() {
            var that = this;
            api.addEventListener({
                name: 'appintent'
            }, function(ret, err) {
                if (ret && ret.appParam.ajpush) {
                    var rep = ret.appParam.ajpush.extra;
                    api.getPrefs({
                        key: 'uid'
                    }, function(res, err) {
                        if (res.value) {
                            that.jp_process(rep, true);
                        } else {
                            api.sendEvent({
                                name: 'onlogin'
                            });
                        }
                    });
                }
            });
            api.addEventListener({
                name: 'noticeclicked'
            }, function(ret, err) {
                if (ret && ret.value) {
                    var rep = ret.value.extra;
                    api.getPrefs({
                        key: 'uid'
                    }, function(res, err) {
                        if (res.value) {
                            ajpush.setBadge({
                                badge: 0
                            })
                            that.jp_process(rep, true);
                        } else {
                            api.sendEvent({
                                name: 'onlogin'
                            });
                        }
                    });
                }
            });
            api.addEventListener({
                name: 'pause'
            }, function(ret, err) {
                ajpush.onPause(); //监听应用进入后台
                that.jp_Resume_Push();
            });
            api.addEventListener({
                name: 'resume'
            }, function(ret, err) {
                ajpush.onResume(); //监听应用恢复到前台
                that.jp_Resume_Push();
            });
        },
        //断开重连
        jp_Resume_Push: function() {
            var that = this;
            if (that.jg_timeout) {
                clearTimeout(that.jg_timeout);
                that.jg_timeout = null;
            }
            setTimeout(function() {
                that.jp_resumePush();
            }, 1000);
        },
        //断开重连
        jp_resumePush: function() {
            var that = this;
            ajpush.isPushStopped(function(ret, err) {
                if (!ret || ret.isStopped != 0) {
                    ajpush.resumePush(function(ret, err) {});
                }
                that.jg_timeout = setTimeout(function() {
                    that.jp_resumePush();
                }, 10 * 1000);
            });
        },
        jp_openwin: function(name, title, params) {
            var that = this;
            api.closeToWin({
                name: 'root'
            });
            setTimeout(function() {
                ns.openWin(name, title, params, {
                    ismain: true
                });
            }, 500);
        },
        jp_process: function(rep, pop_required) {
            var that = this;
            var title = '';
            var name = '';
            var params = {};
            var code = rep.code;
            var type = rep.type;
            if (type == 'wechatmsg_gonggao') {
                if (code == 101) {
                    title = '物业公告';
                    name = 'sqgonggao_detail_frm';
                    params = {
                        title: title,
                        url: name + '.html',
                        id: rep.id
                    }
                }
            }
            if (type == 'wechatmsg_news') {
                if (code == 201) {
                    title = '小区新闻';
                    name = 'xiaoqunews_detail_frm';
                    params = {
                        title: title,
                        url: name + '.html',
                        id: rep.id
                    }
                }
            }
            if (type == 'mallproduct_tuan') {
                if (code == 301) {
                    title = '商品详情';
                    name = 'dl_product_frm';
                    params = {
                        title: title,
                        url: name + '.html',
                        id: rep.id,
                        type: 16
                    }
                }
            }
            if (type == 'mallproduct_xianshi') {
                if (code == 401) {
                    title = '商品详情';
                    name = 'dl_product_frm';
                    params = {
                        title: title,
                        url: name + '.html',
                        id: rep.id,
                        type: 16
                    }
                }
            }
            if (type == 'mallbirthday_coupon') {
                if (code == 501) {
                    title = '我的优惠券';
                    name = 'mycoupon_frm';
                    params = {
                        title: title,
                        url: name + '.html',
                    }
                }
            }
            if (type == 'mallorder_refund') {
                if (code == 601) {
                    title = '订单详情';
                    name = 'ordercomplete_frm';
                    params = {
                        title: title,
                        url: name + '.html',
                        id: rep.id
                    }
                }
            }
            if (type == 'mallorder_shipped') {
                if (code == 701) {
                    title = '订单详情';
                    name = 'ordercomplete_frm';
                    params = {
                        title: title,
                        url: name + '.html',
                        id: rep.id
                    }
                }
            }
            if (type) {
                if (pop_required) {
                    api.confirm({
                        title: '提示',
                        msg: rep.msg,
                        buttons: ['点击查看', '稍后再说']
                    }, function(ret, err) {
                        var index = ret.buttonIndex;
                        if (index == 1) {
                            that.jp_openwin(name, title, params);
                        }
                    });
                } else {
                    that.jp_openwin(name, title, params);
                }
            }
        },
        open_sub_page: function() {
            var that = this;
            if (that.form.id > 0 && that.form.type > 0) {
                var that = this;
                var name = '';
                var title = '';
                if (that.form.type == 1) {
                    name = 'dl_product_frm';
                    title = '商品详情';
                    ns.openWin(name, title, {
                        title: title,
                        url: name + '.html',
                        id: that.form.id,
                        type: 16
                    }, {
                        ismain: true
                    });
                    return;
                }
                if (that.form.type == 2) {
                    name = 'business_frm';
                    title = '商家详情';
                    ns.openWin(name, title, {
                        title: title,
                        url: name + '.html',
                        id: that.form.id,
                        searchtype: 2
                    }, {
                        ismain: true
                    });
                    return;
                }
                if (that.form.type == 3) {
                    name = 'sqgonggao_detail_frm';
                    title = '社区公告';
                    ns.openWin(name, title, {
                        title: title,
                        url: name + '.html',
                        id: that.form.id
                    }, {
                        ismain: true
                    });
                    return;
                }
                if (that.form.type == 4) {
                    name = 'xiaoqunews_detail_frm';
                    title = '小区新闻';
                    ns.openWin(name, title, {
                        title: title,
                        url: name + '.html',
                        id: that.form.id
                    }, {
                        ismain: true
                    });
                    return;
                }
                if (that.form.type == 5) {
                    name = 'servicedetail_frm';
                    title = '生活服务';
                    ns.openWin(name, title, {
                        title: title,
                        url: name + '.html',
                        id: that.form.id
                    }, {
                        ismain: true
                    });
                    return;
                }
            }
        },
        open_room_list: function() {
            var that = this;
            if (that.xiaoqulist.length == 0) {
                ns.openWin('dl_myxiaoqu_frm', '认证', null, {
                    ismain: true,
                });
                return;
            }
            api.sendEvent({
                name: 'open_xiaoqu_list'
            })
        },
        open_searchbar: function() {
            var delay = 0;
            if (api.systemType != 'ios') {
                delay = 100;
            }
            api.openWin({
                name: 'dl_searchbar_frm',
                url: './html/dl_searchbar_frm.html',
                delay: delay,
                slidBackEnabled: false,
                vScrollBarEnabled: false,
                pageParam: {
                    searchtype: 1,
                },
                rect: {
                    x: 0,
                    y: 0,
                    w: 'auto',
                    h: 'auto',
                },
                animation: {
                    type: 'movein',
                    subType: 'from_bottom'
                }
            });
        },
        get_all: function() {
            var that = this;
            setTimeout(function() {
                that.open_menu();
                that.openFrame(app.footers[0]);
            }, 100);
            setTimeout(function() {
                that.jp_init();
            }, 3000);
            setTimeout(function() {
                that.get_cart_count();
            }, 1000)
            var my_chosen_xiaoquname = api.getPrefs({
                sync: true,
                key: 'my_chosen_xiaoquname'
            });
            if (my_chosen_xiaoquname) {
                app.xiaoquname = my_chosen_xiaoquname;
            }
        },
        get_cart_count: function() {
            var that = this;
            var options = {};
            options.action = 'getshoppingcartitemcount';
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    that.cartcount = data.total;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        checkChosenStatus: function() {
            var that = this;
            var my_chosen_xiaoquid = api.getPrefs({
                sync: true,
                key: 'my_chosen_xiaoquid'
            });
            if (!my_chosen_xiaoquid) {
                that.openChooseXiao();
            }
        },
        openChooseXiao: function() {
            ns.openWin('dl_choosexiaoqulist_frm', '选择小区', {}, {
                ismain: true
            });
        }
    }
});
apiready = function() {
    api.parseTapmode();
    app.form.id = api.pageParam.id || 0;
    app.form.type = api.pageParam.type || 0;
    ns = window.Foresight.Util;
    app.device_type = api.systemType;
    ajpush = api.require('ajpush');
    NVTabBar = api.require('NVTabBar');
    ns.confirmPer('storage', function() {})
    app.get_all();
    app.checkChosenStatus();
    api.addEventListener({
        name: 'mall_onclick'
    }, function(ret) {
        NVTabBar.setSelect({
            index: 1,
            selected: true,
        });
        var tabIndex = ret.value.tabIndex || 0;
        app.openFrame(app.footers[1], tabIndex);
        NVTabBar.bringToFront();
        if (tabIndex > 0) {
            var frames = api.frames();
            var isShopFrameOpen = false;
            for (var i = 0; i < frames.length; i++) {
                if (frames[i].name == 'dl_home_shop_frm') {
                    isShopFrameOpen = true;
                    break;
                }
            }
            setTimeout(function() {
                api.sendEvent({
                    name: 'do_change_mall_shop_tab',
                    extra: {
                        tabIndex: tabIndex,
                    }
                });
            }, isShopFrameOpen ? 0 : 1000);
        }
    });
    api.addEventListener({
        name: 'close_intro_success'
    }, function(ret) {
        app.form.id = ret.value.id || 0;
        app.form.type = ret.value.type || '';
        setTimeout(function() {
            app.open_sub_page();
        }, 1000);
    });
    var last_out_time;
    api.addEventListener({
        name: 'keyback'
    }, function(ret, err) {
        if (last_out_time && last_out_time.valueOf() >= (new Date().valueOf() - 1000)) {
            api.closeWidget({
                silent: true
            });
            return;
        }
        last_out_time = new Date();
        api.toast({
            msg: '再按一次退出程序',
            duration: 1000
        });
    });
    api.addEventListener({
        name: 'do_choose_xiaoqu_done'
    }, function(ret) {
        app.xiaoquname = ret.value.name;
    });
    api.addEventListener({
        name: 'do_open_more_service'
    }, function(ret, err) {
        NVTabBar.setSelect({
            index: 2,
            selected: true,
        });
        app.openFrame(app.footers[2]);
        NVTabBar.bringToFront();
    });
    api.addEventListener({
        name: 'onlogin'
    }, function() {
        app.get_all();
    });
    api.addEventListener({
        name: 'do_getshopingcart_count'
    }, function() {
        setTimeout(function() {
            app.get_cart_count();
        }, 500);
    });
};
